package com.ruoyi.coupon.controller;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.coupon.domain.Coupon;
import com.ruoyi.coupon.domain.CouponRecord;
import com.ruoyi.coupon.domain.JsonData;
import com.ruoyi.coupon.enums.BizCodeEnum;
import com.ruoyi.coupon.model.CouponUseVO;
import com.ruoyi.coupon.request.LockCouponRecordRequest;
import com.ruoyi.coupon.service.ICouponRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * couponrecordController
 *
 * @author 林道敬
 * @date 2025-01-02
 */
@RestController
@RequestMapping("/record")
public class CouponRecordController extends BaseController
{
    @Autowired
    private ICouponRecordService couponRecordService;


    @PostMapping("useCoupon")
    public R useCoupon(@RequestBody  CouponUseVO couponUseVO){
        R result = couponRecordService.useCoupon(couponUseVO);
        return result;
    }

    @GetMapping("/queryList")
    public R queryList(){
        QueryWrapper<CouponRecord> couponRecordQueryWrapper = new QueryWrapper<>();
        couponRecordQueryWrapper.eq("user_id",1)
                .le("start_time", LocalDate.now())
                .ge("end_time",LocalDate.now())
                .eq("use_state","NEW");
        List<CouponRecord> list = couponRecordService.list(couponRecordQueryWrapper);
        return R.ok(list);
    }
    @GetMapping("detail/{record_id}")
    public JsonData getCouponRecordDetail(@PathVariable("record_id") long recordId){
        CouponRecord couponRecord = couponRecordService.getById(recordId);
        return couponRecord == null ? JsonData.buildResult(BizCodeEnum.COUPON_NO_EXITS):JsonData.buildSuccess(couponRecord);
    }

    @PostMapping("lock_records")
    public JsonData lockCouponRecords( @RequestBody LockCouponRecordRequest recordRequest){
        JsonData jsonData = couponRecordService.lockCouponRecords(recordRequest);
        System.out.println("jsonData: "+jsonData);
        return jsonData;
    }
    /**
     * 查询couponrecord列表
     */
    @RequiresPermissions("coupon:record:list")
    @GetMapping("/list")
    public TableDataInfo list(CouponRecord couponRecord)
    {
        startPage();
        List<CouponRecord> list = couponRecordService.selectCouponRecordList(couponRecord);
        return getDataTable(list);
    }

    /**
     * 导出couponrecord列表
     */
    @RequiresPermissions("coupon:record:export")
    @Log(title = "couponrecord", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CouponRecord couponRecord)
    {
        List<CouponRecord> list = couponRecordService.selectCouponRecordList(couponRecord);
        ExcelUtil<CouponRecord> util = new ExcelUtil<CouponRecord>(CouponRecord.class);
        util.exportExcel(response, list, "couponrecord数据");
    }

    /**
     * 获取couponrecord详细信息
     */
    @RequiresPermissions("coupon:record:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(couponRecordService.selectCouponRecordById(id));
    }

    /**
     * 新增couponrecord
     */
    @RequiresPermissions("coupon:record:add")
    @Log(title = "couponrecord", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CouponRecord couponRecord)
    {
        return toAjax(couponRecordService.insertCouponRecord(couponRecord));
    }

    /**
     * 修改couponrecord
     */
    @RequiresPermissions("coupon:record:edit")
    @Log(title = "couponrecord", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CouponRecord couponRecord)
    {
        return toAjax(couponRecordService.updateCouponRecord(couponRecord));
    }

    /**
     * 删除couponrecord
     */
    @RequiresPermissions("coupon:record:remove")
    @Log(title = "couponrecord", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(couponRecordService.deleteCouponRecordByIds(ids));
    }
}
